/**
* @fileOverview 工具函数
* @author wind-lc
* @version 1.0
*/

/**
 * 设置localStorage
 * @param {String} key [键名]
 * @param {String} value  [键值]
 * @param {Number} expired [有效时间(天)]
 */
const setLocalStorage = (key, value, expired = 0) => {
  const obj = {
    data: value,
    time: Date.now(),
    expired: 24 * 60 * 60 * 1000 * expired
  }
  localStorage.setItem(key, JSON.stringify(obj))
}
/**
 * 获取localStorage
 * @param {String} key [键名]
 */
const getLocalStorage = key => {
  const obj = JSON.parse(localStorage.getItem(key))
  if (obj === null) {
    return null
  }
  if (obj.expired === 0) {
    return obj.data
  } else {
    if (Date.now() - obj.time > obj.expired) {
      localStorage.removeItem(key)
      return null
    } else {
      return obj.data
    }
  }
}
/**
 * 设置sessionStorage
 * @param {String} key [键名]
 * @param {String} value  [键值]
 */
const setSessionStorage = (key, value) => {
  const obj = {
    data: value
  }
  sessionStorage.setItem(key, JSON.stringify(obj))
}
/**
 * 获取sessionStorage
 * @param {String} key [键名]
 */
const getSessionStorage = key => {
  const obj = JSON.parse(sessionStorage.getItem(key))
  if (obj === null) {
    return null
  }
  return obj.data
}
/**
 * 递归路由
 * @param {Array} path 后台获取的菜单path
 * @param {Object} route 菜单项
 */
const filterRoute = (path, route) => {
  const arr = []
  let temp = []
  if (route.children) {
    route.children.forEach(item => {
      path.forEach(el => {
        const obj = Object.assign({}, item)
        if (el === item.path) {
          temp = filterRoute(path, item)
          if (temp.length > 0) {
            obj.children = temp
          }
          arr.push(obj)
        }
      })
    })
  }
  return arr
}
/**
 * 获取最终页号
 * @param {Number} total 总条数
 * @param {Number} pageNo 页号
 * @param {Number} pageSize 每页条数
 * @param {Number} delNum 删除条数
 */
const getFinalPageNo = (total, pageNo = 1, pageSize = 10, delNum = 1) => {
  const restNum = total - pageSize * (pageNo - 1)
  let pageNoDiff = Math.floor((delNum - restNum) / pageSize) + 1
  pageNoDiff < 0 && (pageNoDiff = 0)
  pageNo = pageNo - pageNoDiff
  pageNo < 1 && (pageNo = 1)
  return pageNo
}
/**
 * 删除树数据的无效children
 * @param {Array} treeData 树数据
 * @param {String} childrenKey 子级集合的key
 */
const deleteInvalidChildren = (treeData, childrenKey = 'children') => {
  treeData = JSON.parse(JSON.stringify(treeData))
  function fnc (data) {
    data.forEach((el) => {
      if (!el[childrenKey] || !el[childrenKey].length) {
        delete el[childrenKey]
      } else {
        fnc(el[childrenKey])
      }
    })
  }
  fnc(treeData)
  return treeData
}
/**
 * 构造自定义树图标的树数据
 * @param {Array} treeData 树数据
 * @param {String} childrenKey 子级集合的key
 */
const getCustomIconTreeData = (treeData, childrenKey = 'children') => {
  treeData = JSON.parse(JSON.stringify(treeData))
  function fnc (tree) {
    tree.forEach((p) => {
      if (p[childrenKey] && (p[childrenKey].length > 0)) {
        p.scopedSlots = {
          icon: 'custom-folder'
        }
        fnc(p[childrenKey])
      } else {
        p.class = 'tree-last-node'
      }
    })
  }
  fnc(treeData)
  return treeData
}
export {
  setLocalStorage,
  getLocalStorage,
  setSessionStorage,
  getSessionStorage,
  filterRoute,
  getFinalPageNo,
  getCustomIconTreeData,
  deleteInvalidChildren
}
